package com.dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;

import com.hibernate.HibernateSessionFactory;
import com.model.Exercises;

public class ExercisesDao {
	public void saveExercises(Exercises exer) {

		Session session = HibernateSessionFactory.getSession();

		try {
			session.beginTransaction();
			session.save(exer);
			session.getTransaction().commit();

		} catch (Exception e) {
			e.printStackTrace();
			session.getTransaction().rollback();
		} finally {
			HibernateSessionFactory.closeSession();
		}
	}
	public List<Exercises> getExercises(){
		Session session = HibernateSessionFactory.getSession();
		Query query = session.createQuery("from Exercises");
		List<Exercises> exers = query.list();
		HibernateSessionFactory.closeSession();
		return exers;
	}
	public int getExerAnswerById(int id){
		Session session = HibernateSessionFactory.getSession();
		session.beginTransaction();
		Exercises exer = (Exercises)session.get(Exercises.class, id);
		return exer.getAnswer();
	}
	public void removeExerById(int id) {
		Session session = HibernateSessionFactory.getSession();
		try {
			session.beginTransaction();
			Exercises exer = (Exercises)session.get(Exercises.class, id);
			session.delete(exer);
			session.getTransaction().commit();
		} catch (Exception e) {
			e.printStackTrace();
			session.getTransaction().rollback();// TODO: handle exception
		}finally{
			HibernateSessionFactory.closeSession();
		}
		
	}
}
